"""
    @Author:  周鑫
    @Description:  开始游戏
    @Date:
    @Version:
"""
"""
更改：
in line 10-20
in line 30 
in line 38-45
in line 47-85
in line 127-136
"""
# 更改1:添加导入
import os
import pygame
from pygame.locals import *

import threading
import time
from sys import exit
import Main_Window
import Image
import Zombie

#zx添加
import zwtj
import choujiang


image=Image.Image()
pygame.mixer.init()#初始化音乐
music_lists=['jspvz/mian_audio/begin_window.mp3','jspvz/mian_audio/Dream Land Days.mp3','jspvz/mian_audio/Centuries-Fall Out Boy.mp3','jspvz/mian_audio/Immortals-Fall Out Boy.mp3']
index=0
pygame.mixer.music.load(music_lists[index])#加载音乐
pygame.mixer.music.set_volume(0.7)	# 设置音量
pygame.mixer.music.play(-1)
buttonclick = pygame.mixer.Sound("jspvz/mian_audio/buttonclick.wav")#按钮音效

# 初始化图像和窗口
image = Image.Image()
window = Main_Window.Window()#创建窗口对象
window.CreateWindow()

#zx添加
window2 = zwtj.Window2()
window2.CreateWindow2()
window3 = choujiang.Window3()
window3.CreateWindow3()


# 更改2：添加字体与文字边框
# 初始化字体
pygame.font.init()
font = pygame.font.Font(None, 36)
# 加载边框图片
script_dir = os.path.dirname(__file__)
border_image_path = os.path.join(script_dir, 'jspvz', 'main_image', '边框.jpg')
border_image = pygame.image.load(border_image_path)

# 更改3:添加击杀数显示与金币数显示
# 更新击杀数的线程函数
def update_kill_count():
    while True:
        time.sleep(0.01)  # 每隔0.01秒刷新一次击杀数
        kill_text = font.render(f'Kills: {Zombie.zombie_kill}', True, (0,0,0))
        text_rect = kill_text.get_rect()
        text_rect.bottomright = (window.pywindow.get_width() - 162, window.pywindow.get_height() - 10)

        window.pywindow.fill((0, 0, 0), text_rect)  # 清除之前的文本背景

        # 绘制边框
        border_margin = 10  # 边框图片相对于文本的间距
        border_rect = text_rect.inflate(border_margin * 2, border_margin * 2)
        scaled_border_image = pygame.transform.scale(border_image, (border_rect.width, border_rect.height))
        window.pywindow.blit(scaled_border_image, border_rect.topleft)

        window.pywindow.blit(kill_text, text_rect)
        pygame.display.update(text_rect)  # 只更新击杀数显示的区域


# 更新金币数的线程函数
def update_money_count():
    while True:
        time.sleep(0.01)  # 每隔0.01秒刷新一次金币数
        money_text = font.render(f'Money: {Zombie.money}', True, (0, 0, 0))
        text_rect = money_text.get_rect()
        text_rect.bottomright = (window.pywindow.get_width() - 5, window.pywindow.get_height() - 10)

        window.pywindow.fill((0, 0, 0), text_rect)  # 清除之前的文本背景

        # 绘制边框
        border_margin = 10  # 边框图片相对于文本的间距
        border_rect = text_rect.inflate(border_margin * 2, border_margin * 2)
        scaled_border_image = pygame.transform.scale(border_image, (border_rect.width, border_rect.height))
        window.pywindow.blit(scaled_border_image, border_rect.topleft)

        window.pywindow.blit(money_text, text_rect)
        pygame.display.update(text_rect)  # 只更新金币数显示的区域

# 更改4:添加启动刷新击杀数和金币数的线程
# 创建并启动刷新击杀数的线程
kill_count_thread = threading.Thread(target=update_kill_count)
kill_count_thread.daemon = True  # 设为守护线程
kill_count_thread.start()

# 创建并启动刷新金币数的线程
money_count_thread = threading.Thread(target=update_money_count)
money_count_thread.daemon = True  # 设为守护线程
money_count_thread.start()

while True:
    if pygame.mixer.music.get_busy() == False:#判断当前是否有背景音乐正在播放
        pygame.mixer.music.play()#播放背景音乐
    #主函数，第一个界面，周鑫应在此处修改首页
    for event in pygame.event.get():
        if event.type == QUIT:  # 接受到退出事件后退出程序
            exit()
        if event.type==KEYDOWN:#按下f开始
            if event.key==K_f:
                print("f已被按下")
                window.PlayGame()
        if event.type == pygame.MOUSEBUTTONDOWN :#检测鼠标按钮事件
            x1,y1=event.pos
            if 470<=x1<=800 and 96<=y1<=242:#判断点击位置
                #点击墓碑的声音
                buttonclick.play()
                begin1 = pygame.image.load(image.PlayGame2).convert_alpha()  # 开始按钮
                window.pywindow.blit(begin1, (470, 96))
                pygame.display.update()
                #.convert_alpha()：将图像转换为包含 alpha 通道的格式，这样可以处理图像的透明部分。
                #按钮点击显示效果
                w, h=pygame.image.load(image.PlayGame1).convert_alpha().get_size()
                time.sleep(0.5)
                #检查事件的位置是否在指定的矩形区域内
                if window.is_rect(event.pos, (470, 100, w, h/2)):
                    pygame.mixer.music.stop()
                    window.PlayGame()

            #zx添加....
            # 点击进入植物图鉴
            if 470 <= x1 <= 800 and 244 <= y1 <= 440:  # 判断鼠标位置
                buttonclick.play()  # 播放音乐
                begin2 = pygame.image.load(image.zwjs).convert_alpha()  # 加载按钮图片
                window.pywindow.blit(begin2, (470, 210))  # 按钮
                pygame.display.update()  # 显示按钮
                w, h = pygame.image.load(image.zwjs).convert_alpha().get_size()  # 获取按钮图片大小
                time.sleep(0.1)  # 延迟反应
                if window.is_rect(event.pos, (470, 240, w, h / 2)):
                    pygame.mixer.music.stop()
                    window2.ZWTJ()  # 进入植物图鉴窗口

            # 抽奖界面
            if 350 <= x1 <= 800 and 450 <= y1 <= 600:  # 判断鼠标位置
                buttonclick.play()  # 播放音乐
                begin3 = pygame.image.load(image.cj).convert_alpha()  # 加载按钮图片
                window.pywindow.blit(begin3, (350, 450))  # 按钮
                pygame.display.update()  # 显示按钮
                w, h = pygame.image.load(image.cj).convert_alpha().get_size()  # 获取按钮图片大小
                time.sleep(0.1)  # 延迟反应
                if window.is_rect(event.pos, (350, 450, w, h / 2)):
                    pygame.mixer.music.stop()
                    window3.CJ()  # 进入抽奖窗口


            if 800 <= x1 <= 900 and 500 <= y1 <= 550:#退出按钮
                if window.is_rect(event.pos, (800,500 ,50 , 50)):
                    exit()
        # 刷新
        pygame.display.update()
    # pygame.display.flip()